KVM libvirt assign static guest IP addresses using DHCP on the virtual machine

 

See all Linux Kernel Based Virtual Machine related FAQs/HowtosSee all Linux Kernel Based Virtual Machine related FAQs/Howtos

I am using KVM/libvirt on Linux operating system and how do I assign static IP address using dnsmasq dhcpd server for my default virtual network switch?

By default, an instance of dnsmasq dhcpd server is automatically configured and started by libvirt for each virtual network switch needing it. Each virtual network switch can given a range of IP addresses provided to guests through DHCP. The default networking switch uses dnsmasq server.

Fig.01: Libvirt uses a program, dnsmasq for DNS and DHCP for default network.Fig.01: Libvirt uses a program, dnsmasq for DNS and DHCP for default network.
 

Libvirt uses a program, dnsmasq for DNS and DHCP for default network.

 

View the current dnsmasq DHCP configuration

Type the following command to list networks
# virsh net-list
Sample outputs:

 Name                 State      Autostart     Persistent

----------------------------------------------------------

 default              active     yes           yes

To see the default network information, enter:
# virsh net-dumpxml default
Sample outputs:

<network connections='2'>

  <name>default</name>

  <uuid>e346291e-f86b-4f2f-a16e-654136441805</uuid>

  <forward mode='nat'>

    <nat>

      <port start='1024' end='65535'/>

    </nat>

  </forward>

  <bridge name='virbr0' stp='on' delay='0'/>

  <mac address='52:54:00:12:fe:35'/>

  <ip address='192.168.122.1' netmask='255.255.255.0'>

    <dhcp>

      <range start='192.168.122.100' end='192.168.122.254'/>

    </dhcp>

  </ip>

</network>

The DHCP range is between 192.168.122.100 and 192.168.122.254.

How to configure static guest IP addresses on the VM host

First find out your guest VM’s MAC addresses, enter:
# virsh dumpxml {VM-NAME-HERE} | grep -i '<mac'
# virsh dumpxml xenial | grep -i '<mac'

Sample outputs:

<mac address='52:54:00:4c:40:1c'/>

Please note down the MAC addresses of the xenial VM that you want to assign static IP addresses.

Edit the default network

Type the following command:
# virsh net-edit default
Find the following section:

 <dhcp>

      <range start='192.168.122.100' end='192.168.122.254'/>

Append the static IP as follows after range:

     <host mac='52:54:00:4c:40:1c' name='xenial' ip='192.168.122.4'/>

Where,

  1. 1.mac='52:54:00:4c:40:1c' – VMs mac address  

  2. 2.name='xenial' – VMs name.  

  3. 3.ip='192.168.122.4' – VMs static IP.  

Here is my complete file with three static DHCP entries for three VMs:

<network>

  <name>default</name>

  <uuid>e346291e-f86b-4f2f-a16e-654136441805</uuid>

  <forward mode='nat'/>

  <bridge name='virbr0' stp='on' delay='0'/>

  <mac address='52:54:00:12:fe:35'/>

  <ip address='192.168.122.1' netmask='255.255.255.0'>

    <dhcp>

      <range start='192.168.122.100' end='192.168.122.254'/>

      <host mac='52:54:00:a0:cc:19' name='centos7' ip='192.168.122.2'/>

      <host mac='52:54:00:f7:a1:c8' name='puffy' ip='192.168.122.3'/>

      <host mac='52:54:00:4c:40:1c' name='xenial' ip='192.168.122.4'/>

    </dhcp>

  </ip>

</network>

Restart DHCP service:
# virsh net-destroy default
# virsh net-start default

Sample outputs:

Network default destroyed

Network default started

If you are running the guest/VM called xenial shutdown it:
# virsh shutdown xenial
# /etc/init.d/libvirt-bin restart
# virsh start xenial
# ping -a 192.168.122.4

Sample outputs:

PING 192.168.122.4 (192.168.122.4) 56(84) bytes of data.

64 bytes from 192.168.122.4: icmp_seq=1 ttl=64 time=0.518 ms

64 bytes from 192.168.122.4: icmp_seq=2 ttl=64 time=0.202 ms

64 bytes from 192.168.122.4: icmp_seq=3 ttl=64 time=0.327 ms

^C

--- 192.168.122.4 ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time 1999ms

rtt min/avg/max/mdev = 0.202/0.349/0.518/0.129 ms

Each time the guest or VM called xenial comes online (or rebooted for the kernel update) it will get 192.168.122.4 as static IP address by dnsmasq DHCP server.